package yxy.game.pm2.module.battle;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.redisson.api.RBucket;

import yxy.apple.util.RandomStringUtility;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.battle.module.record.result.StatisticFighter;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.timeline.Timeline;

public class BattleRecord {

	// bean===========================================================================

	private BattleType type;// 战斗类型
	private String code;// 战斗码
	// 双方人员配置
	private TeamModule teamA;
	private TeamModule teamB;

	// 战斗录像
	private String record;
	private ArrayList<StatisticFighter> statistics;
	private BattleResult battleResult;//赢输和平局
	private BattleResult battleDetailResult;//更详细的战斗结果
	private int round;// 回合数

	private Timeline timeline;

	private boolean skip = true;

	public BattleType getType() {
		return type;
	}

	public void setType(BattleType type) {
		this.type = type;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public TeamModule getTeamA() {
		return teamA;
	}

	public void setTeamA(TeamModule teamA) {
		this.teamA = teamA;
	}

	public TeamModule getTeamB() {
		return teamB;
	}

	public void setTeamB(TeamModule teamB) {
		this.teamB = teamB;
	}

	public String getRecord() {
		return record;
	}

	public void setRecord(String record) {
		this.record = record;
	}

	public ArrayList<StatisticFighter> getStatistics() {
		return statistics;
	}

	public void setStatistics(ArrayList<StatisticFighter> statistics) {
		this.statistics = statistics;
	}


	public BattleResult getBattleDetailResult() {
		return battleDetailResult;
	}

	public void setBattleDetailResult(BattleResult battleDetailResult) {
		this.battleDetailResult = battleDetailResult;
	}

	public BattleResult getBattleResult() {
		return battleResult;
	}

	public void setBattleResult(BattleResult battleResult) {
		this.battleResult = battleResult;
	}

	public int getRound() {
		return round;
	}

	public void setRound(int round) {
		this.round = round;
	}

	public void setTimeline(Timeline timeline) {
		this.timeline = timeline;
	}

	public Timeline getTimeline() {
		return timeline;
	}

	public boolean isSkip() {
		return skip;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	// func===========================================================================
	public Timeline timeline() {
		if (timeline == null) {
			setTimeline(new Timeline());
		}
		return timeline;
	}
	// redis==========================================================================

	private static String key(String recordCode) {
		return String.format("BATTLERECORD:%s", recordCode);
	}

	static protected RedisConnection redis() {
		return RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
	}

	static protected <V> RBucket<V> touch(String key) throws RedisException {
		return RedisManager.client(redis()).getBucket(key);
	}

	public void setv(int expireDays) {
		try {
			RBucket<BattleRecord> bucket = touch(key(getCode()));
			bucket.set(this);
			if (expireDays > 0) {
				bucket.expire(expireDays, TimeUnit.DAYS);
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public BattleRecord getv(String recordCode) {
		try {
			RBucket<BattleRecord> bucket = touch(key(recordCode));
			return bucket.get();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	static public BattleRecord create(String recordCode) {
		BattleRecord module = new BattleRecord();
		module.setCode(recordCode);
		return module;
	}

	public static String randomCode() {
		String timeString = DateTime.now().toString(DateTimeFormat.forPattern("yyyyMMddHHmmss"));
		return randomCode(timeString);
	}

	private static String randomCode(String timeString) {
		String randomCode = RandomStringUtility.randomAlpha(4);
		String code = String.format("btlRec%s%s", timeString, randomCode);
		try {
			if (touch(key(code)).isExists()) {
				code = randomCode(timeString);
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return code;
	}
}
